package com.laicunba.event.listener;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import com.laicunba.event.FriendFirstBuyEvent;
import com.laicunba.pojo.CashCouponDefDO;
import com.laicunba.pojo.FinancingTargetBidDO;
import com.laicunba.pojo.RegistryDO;
import com.laicunba.pojo.TemplateDO;
import com.laicunba.pojo.UserDO;
import com.laicunba.service.CashCouponDefService;
import com.laicunba.service.MobileMessageService;
import com.laicunba.service.RegistryService;
import com.laicunba.service.TemplateService;
import com.laicunba.service.UserService;
import com.laicunba.util.DateUtil;
import com.laicunba.util.StringUtil;

@Component
public class FriendBuyGiveCashCouponListener implements ApplicationListener<FriendFirstBuyEvent> {
	private static final Logger LOG = Logger.getLogger(FriendBuyGiveCashCouponListener.class);
	@Autowired
	private RegistryService registryService;
	@Autowired
	private CashCouponDefService cashCouponDefService;
	@Autowired
	private UserService userService;
	@Autowired
	private TemplateService templateService;
	@Autowired
	private MobileMessageService mobileMessageService;

	@SuppressWarnings({"rawtypes", "unchecked" })
	@Override
	public void onApplicationEvent(FriendFirstBuyEvent event) {
		Map source = (Map) event.getSource();
		UserDO bidUserDO = (UserDO) source.get("bidUserDO");
		// 当前渠道不发送现金券
		if (bidUserDO != null && (bidUserDO.getChannel() != null && bidUserDO.getChannel().startsWith("no-award"))) {
			return;
		}
		UserDO friendDO = (UserDO) source.get("friendDO");
		FinancingTargetBidDO bidDO = (FinancingTargetBidDO) source.get("bidDO");

		if (friendDO == null || bidDO == null) {
			LOG.warn(String.format("触发好友投资送现金券功能,但是 friendDO=%s,bidDO=%s ,其中有一个为空", friendDO, bidDO));
			return;
		}
		if(!"CPB".equalsIgnoreCase(bidDO.getType())) {
			return;
		}
		RegistryDO registryDO = registryService.get(RegistryDO.class, "sys.friend.buy.cashcoupon.open");
		if (registryDO == null) {
			return;
		}
		Date openDate = null;
		CashCouponDefDO defDO = null;
		RegistryDO registry = registryService.get(RegistryDO.class, "invite-info.201707.open");
		if(registry != null &&  StringUtil.isNotEmpty(registry.getVal())) {
			openDate = DateUtil.parse(registry.getVal());
			if(openDate != null && openDate.before(new Date())) {
				defDO = cashCouponDefService.get(CashCouponDefDO.class, "friend-invest");
			} else {
				defDO = cashCouponDefService.get(CashCouponDefDO.class, "friend_buy");
			}
		} else {
			defDO = cashCouponDefService.get(CashCouponDefDO.class, "friend_buy");
		}
		if (defDO == null) {
			LOG.warn("开通了好友投资送现金券功能,但是却找不到现金券模版");
			return;
		}
		cashCouponDefService.send2UserWithMessage(defDO.getId(), friendDO.getId(), false, "邀请好友投资活动");
		TemplateDO templateDO = templateService.findByCodeWidthDefault("MOBILE_MSG_FRIEND_BUY_CASHCOUPON");
		Map paremeter = new HashMap();
		paremeter.put("MobileShortNo", StringUtil.endSubstring(bidUserDO.getMobile(), 4));
		paremeter.put("cashCouponName", defDO.getAmount().toString() + "元现金券");
		String content = templateService.simpleMerge(templateDO.getContent(), paremeter);
		mobileMessageService.sendMsgAsync(friendDO.getMobile(), content);
	}

}
